package jude.java.money.batch.task;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import jude.java.money.batch.Definition;
import jude.java.money.batch.Parameter;
import jude.java.money.batch.memory.TKSites;

/** 查找无效的站 */
public class Cleaner extends BatchTask<Object> {
	
	public Cleaner(Parameter<Object> p) {
		super(p);
	}
	
	@Override
	protected void initFlags() {
		super.initFlags();
		removeFlags(Definition.CONFIG_IGNORE);
	}

	@Override
	protected String getLabel() {
		return "CLS";
	}
	
	private boolean isWordpress() {
		return P.TYPE == Definition.TYPE_WORDPRESS;
	}
	
	private boolean isTK(String site) {
		return site.endsWith(".tk");
	}

	@Override
	protected void execute(String site, String url, int group, int index)
			throws IOException {
		if (isWordpress() && isTK(site) && !TKSites.exists(site)) {
			L.result("Deprecated: " + site);
			return;
		}
		if (retrieved(site)) {
			L.result("UnknownHost: " + site);
			return;
		}
	}
	
	private boolean retrieved(String domain) {
		try {
			InetAddress address = InetAddress.getByName(domain);
			address.getHostAddress();
		} catch (UnknownHostException e) {
			return true;
		}
		return false;
	}

}
